﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using ImageCom.LeadtoolEx;
using ImageEnXLibrary;

namespace ImageCom.ImageEn
{
    public class ImageComEx : IDisposable
    {
        static ImageEnXClass _imageEnX;

        static ImageComEx()
        {
            _imageEnX = new ImageEnXClass();
            _imageEnX.BitmapLocation = 2;//设置成大片模式
            _imageEnX.LegacyBitmap =true;
        }
         /// <summary>
         /// 加载大图dib
         /// </summary>
         /// <param name="inpath"></param>
         /// <returns></returns>
        public static int LoadBigImageToDib(string inpath)
        {
            int intPtr = 0;
            _imageEnX.LoadFromFile(inpath);
            intPtr = _imageEnX.GetImageDIB();
            return intPtr;
        }

        public static DiBImageInfo LoadBigImageToDibObj(string inpath)
        {
            DiBImageInfo rtnInfo = new DiBImageInfo(0, 0);
        
            int intPtr = 0;
            _imageEnX.LoadFromFile(inpath);
            rtnInfo.width = Convert.ToInt32(_imageEnX.GetIOProperty("OriginalWidth"));

            rtnInfo.heigth = Convert.ToInt32(_imageEnX.GetIOProperty("OriginalHeight")); ;
            intPtr = _imageEnX.GetImageDIB();
         
            rtnInfo.Ptr = (IntPtr)intPtr;
            return rtnInfo;
        }

        /// <summary>
          /// 释放DIB资源
          /// </summary>
          /// <param name="intPtr"></param>
        public static void FreeDib(int intPtr)
        {
            _imageEnX.FreeImageDIB(intPtr);
        }

        public void Dispose()
        {
            Marshal.FinalReleaseComObject(_imageEnX);

        }
    }
}
